package top.chmin.project.cloud.sys.select.info;

import java.util.List;
import java.util.ArrayList;
import java.time.LocalDateTime;

import top.chmin.project.cloud.tool.LocalTool;
import top.chmin.project.cloud.common.*;
import lombok.experimental.Accessors;
import lombok.Setter;
import lombok.Getter;



/**
 * chmin
 * 2024-01-26 14:02:25
 * cloud-code
 */
@Getter
@Setter
@Accessors(chain = true)
public class SelectInfo extends Model {
    /*###########auto#start###########*/
    // label字段编码
    private String labelColumnCode;
    // table编号
    private String tableCode;
    // value字段编码
    private String valueColumnCode;
    /*###########auto##end############*/
    /* append $modelFieldAppender$ */

    public SelectInfoModelCondition forInsert() {
        return new SelectInfoModelCondition().forInsert();
    }

    public SelectInfoModelCondition forUpdate() {
        return new SelectInfoModelCondition().forUpdate();
    }

    public SelectInfoModelCondition forDelete() {
        return new SelectInfoModelCondition().forDelete();
    }

    public SelectInfoModelCondition forSelect() {
        return new SelectInfoModelCondition().forSelect();
    }

    public class SelectInfoModelCondition extends Condition {
        SelectInfoModelCondition parent = null;

        private SelectInfoModelCondition() {
        }

        private SelectInfoModelCondition(SelectInfoModelCondition condition) {
            this.parent = condition;
        }

        private SelectInfoModelCondition forInsert() {
            SelectInfoModelCondition condition = new SelectInfoModelCondition();
            List<String> columnNameList = new ArrayList<>();
            List<String> valueList = new ArrayList<>();
            /*###########auto#start###########*/
            columnNameList.add("label_column_code");
            valueList.add("?");
            condition.getValueList().add(getLabelColumnCode());

            columnNameList.add("table_code");
            valueList.add("?");
            condition.getValueList().add(getTableCode());

            columnNameList.add("value_column_code");
            valueList.add("?");
            condition.getValueList().add(getValueColumnCode());

            /*###########auto##end############*/
            /* append $modelForInsertFieldAppender$ */
            condition.getValueList().add(LocalTool.getUserId());
            condition.getValueList().add(LocalTool.getUserId());
            condition.getValueList().add(LocalDateTime.now());
            condition.getValueList().add(LocalDateTime.now());
            condition.setSql("INSERT INTO sys_select_info(%s, create_by, update_by, create_time, update_time) VALUES (%s, ?, ?, ?, ?)".formatted(
                    String.join(", ", columnNameList),
                    String.join(", ", valueList)
            ));
            return condition;
        }

        private SelectInfoModelCondition forUpdate() {
            SelectInfoModelCondition condition = new SelectInfoModelCondition();
            /*###########auto#start###########*/
            if (labelColumnCode != null) {
                condition.getUpdateList().add("label_column_code = ?");
                condition.getUpdateValueList().add(labelColumnCode);
            }

            if (tableCode != null) {
                condition.getUpdateList().add("table_code = ?");
                condition.getUpdateValueList().add(tableCode);
            }

            if (valueColumnCode != null) {
                condition.getUpdateList().add("value_column_code = ?");
                condition.getUpdateValueList().add(valueColumnCode);
            }

            /*###########auto##end############*/
            /* append $modelForUpdateFieldAppender$ */
            if (getUpdateBy() != null) {
                condition.getUpdateList().add("update_by = ?");
                condition.getValueList().add(LocalTool.getUserId());
            }
            if (getUpdateTime() != null) {
                condition.getUpdateList().add("update_time = ?");
                condition.getValueList().add(LocalDateTime.now());
            }
            condition.setSql("UPDATE sys_select_info#update##where##order##limit#");
            return condition;
        }

        private SelectInfoModelCondition forDelete() {
            SelectInfoModelCondition condition = new SelectInfoModelCondition();
            condition.setSql("DELETE FROM sys_select_info#where##group##order##limit#");
            return condition;
        }

        private SelectInfoModelCondition forSelect() {
            SelectInfoModelCondition condition = new SelectInfoModelCondition();
            condition.modelClass = SelectInfo.class;
            condition.setSql("SELECT #select# FROM sys_select_info#where##group##order##limit#");
            return condition;
        }

        public SelectInfoModelCondition distinct() {
            this.distinct = true;
            return this;
        }

        public SelectInfoModelCondition limit(int limit) {
            this.limit = " LIMIT " + limit;
            return this;
        }

        public SelectInfoModelCondition sub() {
            return new SelectInfoModelCondition(this);
        }

        public SelectInfoModelCondition finish() {
            if (!whereList.isEmpty()) {
                parent.whereList.add("(" + String.join(" OR ", whereList) + ")");
            }
            parent.whereValueList.addAll(whereValueList);
            return parent;
        }

        public SelectInfoCondition toFlex() {
            SelectInfoCondition flex = new SelectInfoCondition();
            flex.setSql(sql);
            flex.setDistinct(distinct);
            flex.setModelClass(modelClass);
            flex.setSelectList(selectList);
            flex.setUpdateList(updateList);
            flex.setUpdateValueList(updateValueList);
            flex.setWhereList(whereList);
            flex.setWhereValueList(whereValueList);
            flex.setGroupList(groupList);
            flex.setOrderList(orderList);
            flex.setLimit(limit);
            return flex;
        }

        /*###########auto#start###########*/
        public CreateBy createBy() {
            return new CreateBy();
        }

        public class CreateBy extends StringModelColumn<SelectInfoModelCondition> {
            private CreateBy() {
                super(SelectInfoModelCondition.this, "create_by", SelectInfo.this.getCreateBy());
            }
        }

        public CreateTime createTime() {
            return new CreateTime();
        }

        public class CreateTime extends LocalDateTimeModelColumn<SelectInfoModelCondition> {
            private CreateTime() {
                super(SelectInfoModelCondition.this, "create_time", SelectInfo.this.getCreateTime());
            }
        }

        public Id id() {
            return new Id();
        }

        public class Id extends IntegerModelColumn<SelectInfoModelCondition> {
            private Id() {
                super(SelectInfoModelCondition.this, "id", SelectInfo.this.getId());
            }
        }

        public LabelColumnCode labelColumnCode() {
            return new LabelColumnCode();
        }

        public class LabelColumnCode extends StringModelColumn<SelectInfoModelCondition> {
            private LabelColumnCode() {
                super(SelectInfoModelCondition.this, "label_column_code", SelectInfo.this.getLabelColumnCode());
            }
        }

        public TableCode tableCode() {
            return new TableCode();
        }

        public class TableCode extends StringModelColumn<SelectInfoModelCondition> {
            private TableCode() {
                super(SelectInfoModelCondition.this, "table_code", SelectInfo.this.getTableCode());
            }
        }

        public UpdateBy updateBy() {
            return new UpdateBy();
        }

        public class UpdateBy extends StringModelColumn<SelectInfoModelCondition> {
            private UpdateBy() {
                super(SelectInfoModelCondition.this, "update_by", SelectInfo.this.getUpdateBy());
            }
        }

        public UpdateTime updateTime() {
            return new UpdateTime();
        }

        public class UpdateTime extends LocalDateTimeModelColumn<SelectInfoModelCondition> {
            private UpdateTime() {
                super(SelectInfoModelCondition.this, "update_time", SelectInfo.this.getUpdateTime());
            }
        }

        public ValueColumnCode valueColumnCode() {
            return new ValueColumnCode();
        }

        public class ValueColumnCode extends StringModelColumn<SelectInfoModelCondition> {
            private ValueColumnCode() {
                super(SelectInfoModelCondition.this, "value_column_code", SelectInfo.this.getValueColumnCode());
            }
        }

        /*###########auto##end############*/
        /* append $modelConditionFieldAppender$ */
    }
}
